{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a5cf468e-ac33-4577-92c2-508a50080ac7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "76181f6b-f5cb-4c41-a559-0df32afb0b44",
   "metadata": {},
   "outputs": [],
   "source": [
    "client = OpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "48e5bd21-e357-4b4b-98c4-8aab3232732b",
   "metadata": {},
   "outputs": [],
   "source": [
    "file = client.files.create(\n",
    "    file=open(\"论文介绍.pdf\", \"rb\"),\n",
    "    purpose=\"assistants\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2a4f2cae-b6d7-4f4f-95b0-15d130b38175",
   "metadata": {},
   "outputs": [],
   "source": [
    "assistant = client.beta.assistants.create(\n",
    "    model=\"gpt-3.5-turbo\",\n",
    "    name=\"AI论文问答助手\",\n",
    "    instructions=\"你是一个智能助手，可以访问文件来回答人工智能领域论文的相关问题。\",\n",
    "    tools=[{\"type\": \"retrieval\"}],\n",
    "    file_ids=[file.id]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e9c81556-8cd1-40cb-a3d8-152fa3d26d6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "thread = client.beta.threads.create()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ab9d851c-5849-479e-b629-9214dd44e921",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_response_from_assistant(assistant, thread, prompt, run_instruction=\"\"):\n",
    "    message = client.beta.threads.messages.create(\n",
    "    thread_id=thread.id,\n",
    "    role=\"user\",\n",
    "    content=prompt\n",
    "    )\n",
    "    \n",
    "    run = client.beta.threads.runs.create(\n",
    "      thread_id=thread.id,\n",
    "      assistant_id=assistant.id,\n",
    "      instructions=run_instruction\n",
    "    )\n",
    "    \n",
    "    while run.status != \"completed\":\n",
    "        keep_retrieving_run = client.beta.threads.runs.retrieve(\n",
    "            thread_id=thread.id,\n",
    "            run_id=run.id\n",
    "        )\n",
    "        print(f\"Run status: {keep_retrieving_run.status}\")\n",
    "\n",
    "        if keep_retrieving_run.status == \"completed\":\n",
    "            break\n",
    "    \n",
    "    messages = client.beta.threads.messages.list(\n",
    "        thread_id=thread.id\n",
    "    )\n",
    "    \n",
    "    for data in messages.data:\n",
    "        print(\"\\n\")\n",
    "        print(data.content[0].text.value)\n",
    "        print(\"------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "865d1fcf-cb0c-4c83-bcc6-fca998e8cb7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: in_progress\n",
      "Run status: completed\n",
      "\n",
      "\n",
      "介绍了Transformer架构的论文标题是\"Attention Is All You Need\"，论文链接为：[https://arxiv.org/pdf/1706.03762.pdf](https://arxiv.org/pdf/1706.03762.pdf)【5†source】。\n",
      "------\n",
      "\n",
      "\n",
      "哪篇论文介绍了Transformer架构？论文链接是什么？\n",
      "------\n"
     ]
    }
   ],
   "source": [
    "get_response_from_assistant(assistant, thread, \"哪篇论文介绍了Transformer架构？论文链接是什么？\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba9f2d57-7e97-49ce-a38b-8e828091460d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
